﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data.Common;
using System.Data;
using System.Data.OleDb;

/// <summary>
///DBBlogAccount 的摘要说明
/// </summary>
public class DBBlogAccount
{
    public DBBlogAccount()
    {

    }

    public bool Add(string account, string des, string accessToken, DateTime expireIn)
    {
        DbCommand command = DBConfig.CreateCommand("Insert Into [blogAccount](account, weiboDes, accessToken, expireIn) Values(@account, @des, @accessToken, @expireIn)");
        command.Parameters.AddRange(
            new DbParameter[]
            {
                DBConfig.CreateParameter("@account", account),
                DBConfig.CreateParameter("@des", des),
                DBConfig.CreateParameter("@accessToken", accessToken),
                DBConfig.CreateParameter("@expireIn", "#" + expireIn + "#", OleDbType.DBTimeStamp)
            }
            );
        using (DbConnection conn = DBConfig.CreateConnect())
        {
            command.Connection = conn;
            conn.Open();
            return command.ExecuteNonQuery() > 0;
        }
    }

    public bool Delete(int id)
    {
        DbCommand command = DBConfig.CreateCommand("Delete FROM [blogAccount] WHERE id= @id");
        command.Parameters.Add(DBConfig.CreateParameter("@id", id));
        using (DbConnection conn = DBConfig.CreateConnect())
        {
            command.Connection = conn;
            conn.Open();
            return command.ExecuteNonQuery() > 0;
        }
    }

    public BlogAccountData Select(string account)
    {
        DbCommand command = DBConfig.CreateCommand("Select * From [blogAccount] Where account=@account");
        command.Parameters.Add(DBConfig.CreateParameter("@account", account));
        using (DbConnection conn = DBConfig.CreateConnect())
        {
            command.Connection = conn;
            conn.Open();
            DbDataReader reader = command.ExecuteReader();
            if (reader.Read())
            {
                BlogAccountData result = new BlogAccountData();
                result.id = reader.GetInt32(0);
                result.account = reader.GetString(1);
                result.des = reader.GetString(2);
                result.accessToken = reader.GetString(3);
                return result;
            }
            return null;
        }
    }

    public BlogAccountData Select(int id)
    {
        DbCommand command = DBConfig.CreateCommand("Select * From [blogAccount] Where id=@id");
        command.Parameters.Add(DBConfig.CreateParameter("@id", id));
        using (DbConnection conn = DBConfig.CreateConnect())
        {
            command.Connection = conn;
            conn.Open();
            DbDataReader reader = command.ExecuteReader();
            if (reader.Read())
            {
                BlogAccountData result = new BlogAccountData();
                result.id = reader.GetInt32(0);
                result.account = reader.GetString(1);
                result.des = reader.GetString(2);
                result.accessToken = reader.GetString(3);
                result.expireIn = reader.GetDateTime(4);
                return result;
            }
            return null;
        }
    }

    public bool Update(string account, string des, string accessToken, DateTime expireIn)
    {
        DbCommand command = DBConfig.CreateCommand("Update [blogAccount] set account=@account, weiboDes=@weiboDes, accessToken=@accessToken, expireIn=@expireIn");
        command.Parameters.AddRange(
                new DbParameter[]
                {
                    DBConfig.CreateParameter("@account", account),
                    DBConfig.CreateParameter("@des", des),
                    DBConfig.CreateParameter("@accessToken", accessToken),
                    DBConfig.CreateParameter("@expireIn", "#" + expireIn + "#", OleDbType.DBTimeStamp)
                }
                );
        using (DbConnection conn = DBConfig.CreateConnect())
        {
            command.Connection = conn;
            conn.Open();
            return command.ExecuteNonQuery() > 0;
        }
    }

    public List<BlogAccountData> SelectList()
    {
        DbCommand command = DBConfig.CreateCommand("Select Top 300 * From [blogAccount]");
        using (DbConnection conn = DBConfig.CreateConnect())
        {
            command.Connection = conn;
            conn.Open();
            DbDataReader reader = command.ExecuteReader();
            List<BlogAccountData> result = new List<BlogAccountData>();
            while (reader.Read())
            {
                BlogAccountData data = new BlogAccountData();
                data.id = reader.GetInt32(0);
                data.account = reader.GetString(1);
                data.des = reader.GetString(2);
                data.accessToken = reader.GetString(3);
                result.Add(data);
            }
            return result;
        }
    }
}